```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java中sleep()与wait()的区别 | 技术小馆</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
            color: #333;
            line-height: 1.8;
        }
        .article-content h1, .article-content h2, .article-content h3 {
            font-family: 'Noto Serif SC', Georgia, 'Times New Roman', Times, serif;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
        }
        .code-block {
            background-color: #2d2d2d;
            border-radius: 8px;
            padding: 1.5rem;
            position: relative;
        }
        .code-block pre {
            margin: 0;
            color: #f8f8f2;
            font-family: 'Courier New', Courier, monospace;
            font-size: 0.9rem;
            line-height: 1.5;
        }
        .code-block .copy-btn {
            position: absolute;
            top: 0.5rem;
            right: 0.5rem;
            background: rgba(255,255,255,0.1);
            border: none;
            color: white;
            padding: 0.25rem 0.5rem;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.2s;
        }
        .code-block .copy-btn:hover {
            background: rgba(255,255,255,0.2);
        }
        .comparison-card {
            transition: all 0.3s ease;
            border-radius: 12px;
            overflow: hidden;
        }
        .comparison-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0,0,0,0.1);
        }
        .mermaid svg {
            display: block;
            margin: 0 auto;
        }
        .feature-icon {
            font-size: 1.5rem;
            margin-bottom: 1rem;
            color: #6e8efb;
        }
        .text-highlight {
            background: linear-gradient(120deg, rgba(110,142,251,0.2) 0%, rgba(167,119,227,0.2) 100%);
            padding: 0.1rem 0.3rem;
            border-radius: 4px;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4">
        <div class="max-w-4xl mx-auto text-center">
            <h1 class="text-4xl md:text-5xl font-bold mb-6">Java中sleep()与wait()的区别</h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">深入理解多线程编程中的线程控制机制</p>
            <div class="flex justify-center space-x-4">
                <a href="#overview" class="bg-white text-indigo-600 px-6 py-3 rounded-full font-medium hover:bg-indigo-100 transition duration-300">开始阅读</a>
                <a href="#comparison" class="border border-white text-white px-6 py-3 rounded-full font-medium hover:bg-white hover:bg-opacity-10 transition duration-300">直接比较</a>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="max-w-4xl mx-auto px-4 py-12">
        <!-- Introduction Section -->
        <section id="overview" class="mb-16">
            <div class="bg-white rounded-xl shadow-lg p-8 mb-8">
                <h2 class="text-3xl font-bold mb-6 text-gray-800">多线程编程中的线程控制</h2>
                <p class="text-lg text-gray-700 mb-6">在多线程编程中，线程的控制和协调是一个至关重要的课题。合理地管理线程的执行与等待，不仅可以提高程序的性能，还能有效避免潜在的并发问题。</p>
                
                <div class="bg-blue-50 border-l-4 border-blue-500 p-4 mb-6 rounded-r">
                    <p class="text-blue-800"><i class="fas fa-lightbulb text-blue-500 mr-2"></i> 想象一下你在开发一个复杂的应用，比如一个在线购物系统。在这个系统中，可能会有多个用户同时访问，进行下单、支付等操作。在这样的环境中，如何有效地管理线程的状态，确保数据的一致性和完整性，就显得尤为重要。</p>
                </div>
                
                <p class="text-gray-700 mb-4"><span class="text-highlight">sleep()</span>方法用于让当前线程暂停执行一段时间，这在一些需要定时操作的场景中非常有用，比如在处理定时任务或轮询操作时。</p>
                <p class="text-gray-700"><span class="text-highlight">wait()</span>方法则是为了线程间的协调与通信，它使当前线程在某个条件不满足时进入等待状态，直到另一个线程调用<span class="text-highlight">notify()</span>或<span class="text-highlight">notifyAll()</span>方法来唤醒它。这样可以有效实现线程之间的协作，确保资源的合理利用。</p>
            </div>
            
            <div class="flex justify-center mb-8">
                <img src="https://cdn.nlark.com/yuque/0/2024/png/21449790/1721187133782-16c26e92-661a-402b-962f-313a5c5be6f9.png" alt="多线程示意图" class="rounded-lg shadow-md max-w-full h-auto">
            </div>
        </section>

        <!-- Definition Section -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 border-b pb-2">定义与基本用法</h2>
            
            <div class="flex justify-center mb-8">
                <img src="https://cdn.nlark.com/yuque/0/2024/png/21449790/1721187474102-2454ab5c-00c4-407b-a6ac-1f77264e30ab.png" alt="sleep vs wait" class="rounded-lg shadow-md max-w-full h-auto">
            </div>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <!-- Sleep Card -->
                <div class="bg-white rounded-xl shadow-lg p-6 comparison-card">
                    <div class="flex items-center mb-4">
                        <div class="bg-indigo-100 rounded-full p-3 mr-4">
                            <i class="fas fa-moon text-indigo-600"></i>
                        </div>
                        <h3 class="text-2xl font-bold text-gray-800">sleep()</h3>
                    </div>
                    <p class="text-gray-700 mb-4"><span class="text-highlight">sleep()</span>是<span class="text-highlight">Thread</span>类的一个静态方法，用于让当前线程暂停执行指定的时间。</p>
                    
                    <div class="code-block mb-4">
                        <button class="copy-btn" onclick="copyCode(this)"><i class="fas fa-copy"></i></button>
                        <pre><code>public static native void sleep(long millis) 
    throws InterruptedException;</code></pre>
                    </div>
                    
                    <div class="space-y-3">
                        <div class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <p class="text-gray-700"><strong>暂停当前线程</strong>：调用后线程将进入休眠状态，暂停执行指定的毫秒数</p>
                        </div>
                        <div class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <p class="text-gray-700"><strong>保持锁</strong>：在休眠期间保持对所有已获得锁的占有</p>
                        </div>
                        <div class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <p class="text-gray-700"><strong>自动恢复</strong>：线程会在指定时间过后自动恢复执行</p>
                        </div>
                    </div>
                    
                    <div class="mt-6">
                        <h4 class="font-bold text-gray-800 mb-2">示例代码：</h4>
                        <div class="code-block">
                            <button class="copy-btn" onclick="copyCode(this)"><i class="fas fa-copy"></i></button>
                            <pre><code>public class SleepDemo {
    public static void main(String[] args) {
        try {
            System.out.println("线程将休眠2秒.");
            Thread.sleep(2000); // 暂停当前线程2秒
            System.out.println("线程被唤醒.");
        } catch (InterruptedException e) {
            System.out.println("线程中断了。");
        }
    }
}</code></pre>
                        </div>
                    </div>
                </div>
                
                <!-- Wait Card -->
                <div class="bg-white rounded-xl shadow-lg p-6 comparison-card">
                    <div class="flex items-center mb-4">
                        <div class="bg-purple-100 rounded-full p-3 mr-4">
                            <i class="fas fa-clock text-purple-600"></i>
                        </div>
                        <h3 class="text-2xl font-bold text-gray-800">wait()</h3>
                    </div>
                    <p class="text-gray-700 mb-4"><span class="text-highlight">wait()</span>是<span class="text-highlight">Object</span>类的方法，用于线程间的协调与通信。</p>
                    
                    <div class="code-block mb-4">
                        <button class="copy-btn" onclick="copyCode(this)"><i class="fas fa-copy"></i></button>
                        <pre><code>public final void wait() throws InterruptedException;
public final void wait(long timeout) throws InterruptedException;</code></pre>
                    </div>
                    
                    <div class="space-y-3">
                        <div class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <p class="text-gray-700"><strong>释放锁并等待</strong>：调用时线程必须持有对象的监视器锁，调用后会释放锁</p>
                        </div>
                        <div class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <p class="text-gray-700"><strong>线程协作</strong>：常用于实现生产者-消费者模式等线程间协调场景</p>
                        </div>
                        <div class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <p class="text-gray-700"><strong>显式唤醒</strong>：需要其他线程调用<span class="text-highlight">notify()</span>或<span class="text-highlight">notifyAll()</span>来唤醒</p>
                        </div>
                    </div>
                    
                    <div class="mt-6">
                        <h4 class="font-bold text-gray-800 mb-2">示例代码：</h4>
                        <div class="code-block">
                            <button class="copy-btn" onclick="copyCode(this)"><i class="fas fa-copy"></i></button>
                            <pre><code>public class WaitDemo {
    private final Object lock = new Object();

    public void waitingMethod() {
        synchronized (lock) {
            try {
                System.out.println("线程正在等待。");
                lock.wait(); // 释放锁并等待
                System.out.println("线程处于唤醒状态。");
            } catch (InterruptedException e) {
                System.out.println("线程中断了。");
            }
        }
    }
}</code></pre>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Comparison Section -->
        <section id="comparison" class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 border-b pb-2">关键区别对比</h2>
            
            <!-- Feature Comparison -->
            <div class="grid md:grid-cols-3 gap-6 mb-12">
                <div class="bg-white rounded-xl shadow-lg p-6 text-center">
                    <div class="feature-icon">
                        <i class="fas fa-cubes"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3 text-gray-800">调用对象</h3>
                    <div class="text-left space-y-4">
                        <div>
                            <h4 class="font-medium text-indigo-600 mb-1">sleep()</h4>
                            <p class="text-gray-700">Thread类的静态方法，不依赖于对象锁</p>
                        </div>
                        <div>
                            <h4 class="font-medium text-purple-600 mb-1">wait()</h4>
                            <p class="text-gray-700">Object类的方法，必须在同步代码块中调用</p>
                        </div>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl shadow-lg p-6 text-center">
                    <div class="feature-icon">
                        <i class="fas fa-lock"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3 text-gray-800">锁的行为</h3>
                    <div class="text-left space-y-4">
                        <div>
                            <h4 class="font-medium text-indigo-600 mb-1">sleep()</h4>
                            <p class="text-gray-700">不释放任何锁，保持锁的占有</p>
                        </div>
                        <div>
                            <h4 class="font-medium text-purple-600 mb-1">wait()</h4>
                            <p class="text-gray-700">释放调用对象的锁，允许其他线程访问</p>
                        </div>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl shadow-lg p-6 text-center">
                    <div class="feature-icon">
                        <i class="fas fa-bell"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3 text-gray-800">恢复方式</h3>
                    <div class="text-left space-y-4">
                        <div>
                            <h4 class="font-medium text-indigo-600 mb-1">sleep()</h4>
                            <p class="text-gray-700">时间到后自动恢复</p>
                        </div>
                        <div>
                            <h4 class="font-medium text-purple-600 mb-1">wait()</h4>
                            <p class="text-gray-700">需要其他线程调用notify()/notifyAll()</p>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- Mermaid Diagram -->
            <div class="bg-white rounded-xl shadow-lg p-6 mb-12">
                <h3 class="text-2xl font-bold mb-6 text-gray-800">sleep()与wait()工作流程对比</h3>
                <div class="mermaid">
                    graph TD
                        A[调用sleep()] --> B[线程进入休眠状态]
                        B --> C[保持所有锁]
                        C --> D[时间到自动恢复]
                        
                        E[调用wait()] --> F[释放对象锁]
                        F --> G[线程进入等待状态]
                        G --> H{唤醒条件}
                        H -->|notify()/notifyAll()| I[重新获取锁并继续执行]
                </div>
            </div>
            
            <!-- Example Code Section -->
            <div class="bg-white rounded-xl shadow-lg p-8">
                <h3 class="text-2xl font-bold mb-6 text-gray-800">实践示例</h3>
                <div class="code-block mb-6">
                    <button class="copy-btn" onclick="copyCode(this)"><i class="fas fa-copy"></i></button>
                    <pre><code>public static void main(String[] args) throws IOException {
    SleepDemo sleepDemo = new SleepDemo();
    new Thread(() -> {
        sleepDemo.test();
    }, "线程A").start();
    new Thread(() -> {
        sleepDemo.test();
    }, "线程B").start();
}

private void test() {
    synchronized (this) {
        try {
            System.out.println(Thread.currentThread().getName() + " -->进入到test放了");
            wait(); // 释放锁
            System.out.println(Thread.currentThread().getName() + " -->执行结束");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}</code></pre>
                </div>
                
                <div class="bg-yellow-50 border-l-4 border-yellow-400 p-4 rounded-r">
                    <p class="text-yellow-800"><i class="fas fa-exclamation-triangle text-yellow-500 mr-2"></i> <strong>注意：</strong>在这个示例中，两个线程会依次执行test()方法，但当调用wait()时，线程会释放锁并等待，直到其他线程调用notify()或notifyAll()来唤醒它们。如果没有唤醒操作，线程将一直处于等待状态。</p>
                </div>
            </div>
        </section>
        
        <!-- Exception Handling Section -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 border-b pb-2">异常处理</h2>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white rounded-xl shadow-lg p-6">
                    <h3 class="text-2xl font-bold mb-4 text-gray-800">sleep()的异常处理</h3>
                    <p class="text-gray-700 mb-4"><span class="text-highlight">sleep()</span>方法可能抛出<span class="text-highlight">InterruptedException</span>，这通常发生在当前线程在休眠期间被其他线程中断。</p>
                    
                    <div class="code-block">
                        <button class="copy-btn" onclick="copyCode(this)"><i class="fas fa-copy"></i></button>
                        <pre><code>public class SleepDemo {
    public static void main(String[] args) {
        try {
            System.out.println("线程将在2秒后恢复执行。");
            Thread.sleep(2000);
            System.out.println("线程恢复执行。");
        } catch (InterruptedException e) {
            System.out.println("线程被中断，处理相应逻辑。");
            // 处理线程中断的逻辑
        }
    }
}</code></pre>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl shadow-lg p-6">
                    <h3 class="text-2xl font-bold mb-4 text-gray-800">wait()的异常处理</h3>
                    <p class="text-gray-700 mb-4"><span class="text-highlight">wait()</span>方法同样可能抛出<span class="text-highlight">InterruptedException</span>，是在等待期间被中断时发生。</p>
                    
                    <div class="code-block">
                        <button class="copy-btn" onclick="copyCode(this)"><i class="fas fa-copy"></i></button>
                        <pre><code>public class WaitDemo {
    private final Object lock = new Object();

    public void waitingMethod() {
        synchronized (lock) {
            try {
                System.out.println("线程进入等待状态。");
                lock.wait();
                System.out.println("线程被唤醒。");
            } catch (InterruptedException e) {
                System.out.println("线程在等待期间被中断，处理相应逻辑。");
                // 处理线程中断的逻辑
            }
        }
    }
}</code></pre>
                    </div>
                </div>
            </div>
        </section>
        
        <!-- Summary Section -->
        <section class="bg-white rounded-xl shadow-lg p-8 mb-16">
            <h2 class="text-3xl font-bold mb-6 text-gray-800">总结</h2>
            
            <div class="space-y-6">
                <div class="flex items-start">
                    <div class="bg-indigo-100 p-2 rounded-full mr-4">
                        <i class="fas fa-check text-indigo-600"></i>
                    </div>
                    <div>
                        <h3 class="text-xl font-bold mb-2 text-gray-800">何时使用sleep()</h3>
                        <p class="text-gray-700">当你需要线程暂停执行一段时间，且不需要与其他线程协调时使用<span class="text-highlight">sleep()</span>。典型场景包括定时任务、轮询操作或人为延迟。</p>
                    </div>
                </div>
                
                <div class="flex items-start">
                    <div class="bg-purple-100 p-2 rounded-full mr-4">
                        <i class="fas fa-check text-purple-600"></i>
                    </div>
                    <div>
                        <h3 class="text-xl font-bold mb-2 text-gray-800">何时使用wait()</h3>
                        <p class="text-gray-700">当你需要线程间协调，某个线程需要等待某个条件满足时使用<span class="text-highlight">wait()</span>。典型场景包括生产者-消费者模式、资源池管理等。</p>
                    </div>
                </div>
                
                <div class="bg-blue-50 border-l-4 border-blue-500 p-4 rounded-r">
                    <p class="text-blue-800"><i class="fas fa-info-circle text-blue-500 mr-2"></i> 记住：<span class="text-highlight">sleep()</span>是线程级别的控制，而<span class="text-highlight">wait()</span>是对象级别的线程协调机制。正确使用它们可以显著提高多线程程序的效率和可靠性。</p>
                </div>
            </div>
        </section>
    </div>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-12 px-4">
        <div class="max-w-4xl mx-auto">
            <div class="flex flex-col items-center text-center">
                <h3 class="text-2xl font-bold mb-4 text-white">技术小馆</h3>
                <p class="mb-6 max-w-md">探索技术奥秘，分享编程心得</p>
                <a href="http://www.yuque.com/jtostring" class="text-indigo-400 hover:text-indigo-300 transition duration-300">www.yuque.com/jtostring</a>
            </div>
        </div>
    </footer>

    <script>
        // Initialize Mermaid
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        // Copy code button functionality
        function copyCode(button) {
            const codeBlock = button.parentElement;
            const code = codeBlock.querySelector('code').textContent;
            navigator.clipboard.writeText(code).then(() => {
                button.innerHTML = '<i class="fas fa-check"></i>';
                setTimeout(() => {
                    button.innerHTML = '<i class="fas fa-copy"></i>';
                }, 2000);
            });
        }
        
        // Smooth scrolling for anchor links
        document.querySelectorAll('a[href^="#"]').forEach(anchor => {
            anchor.addEventListener('click', function (e) {
                e.preventDefault();
                document.querySelector(this.getAttribute('href')).scrollIntoView({
                    behavior: 'smooth'
                });
            });
        });
    </script>
</body>
</html>
```